package com.drivemanage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.drivemanage.dao.LogDao;
import com.drivemanage.entry.DriveResult;
import com.drivemanage.entry.LogInfo;
import com.drivemanage.utils.OpenUtils;

/**
 * 登录操作的业务处理类
 */
@Service
// 追加事务管理
@Transactional
public class LoginServiceImpl implements LoginService {

    Logger log = Logger.getLogger("LoginServiceImpl");

    @Resource
    private LogDao logDao;

    /**
     * 用户登录
     */
    @Override
    public DriveResult login(String user_Id, String pass_word) {
        // 1.校验是否存在
        LogInfo lofos = logDao.qryUserInfo(user_Id);
        if (OpenUtils.isEmpty(lofos)) {
            return new DriveResult(DriveResult.ERROR_CODE, "用户名不存在，请先注册！", "");
        }
        String password = lofos.getPass_word();
        if (!password.equals(pass_word)) {
            return new DriveResult(DriveResult.ERROR_CODE, "用户名或密码错误!", "");
        }
        Map resultMap = new HashMap();
        resultMap.put("userName", lofos.getReal_name());
        resultMap.put("is_full", lofos.getIs_full());
        resultMap.put("userType", lofos.getUser_type());
        return new DriveResult(DriveResult.SUCCESS_CODE, "登录成功！", resultMap);
    }

    /**
     * 注册用户
     */
    @Override
    public DriveResult resIn(String userid, String pwd) {
        // 1.校验是否存在
        LogInfo lofos = logDao.qryUserInfo(userid);
        if (!OpenUtils.isEmpty(lofos)) {
            return new DriveResult(DriveResult.ERROR_CODE, "用户名已存在", "");
        }
        // 设置为未完善，非黑名单用户.用户类型为普通用户
        LogInfo logparam = new LogInfo(userid, pwd, "0", "", "", "", "", "0", OpenUtils.getSysDate(), "0", "");
        logDao.createUser(logparam);
        return new DriveResult(DriveResult.SUCCESS_CODE, "注册成功", "");
    }

    /**
     * 修改和完善信息
     */
    @Override
    public DriveResult modifyInfo(String userid, String realname, String certnum, String linkphone, String drivenum,
            String driveyear) {
        LogInfo logparam = new LogInfo(userid, "", "", realname, certnum, linkphone, drivenum, "1", "", "", driveyear);
        logDao.modifyInfo(logparam);
        return new DriveResult(DriveResult.SUCCESS_CODE, "修改成功", realname);
    }

    /**
     * 获取用户信息
     */
    @Override
    public DriveResult getUserInfo(String userid) {
        LogInfo lofo = logDao.qryUserInfo(userid);
        List<LogInfo> lofos = new ArrayList<LogInfo>();
        lofos.add(lofo);
        return new DriveResult(DriveResult.SUCCESS_CODE, "修改成功", lofos);
    }

    /**
     * 修改密码
     */
    @Override
    public DriveResult modifyPwd(String userid, String oldPwd, String newPwd) {
        // 先查询
        LogInfo logs = logDao.qryUserInfo(userid);
        if (!oldPwd.equals(logs.getPass_word())) {
            return new DriveResult(DriveResult.ERROR_CODE, "原始密码错误，请重新输入！", "");
        }
        Map param = OpenUtils.asMap("userid", userid, "newPwd", newPwd);
        logDao.modifyPwd(param);
        return new DriveResult(DriveResult.SUCCESS_CODE, "修改成功", "");
    }

}
